<?php


BLoader::Import('../plugin/database', true);

class BPlugin_database_insert extends BPlugin_database_
{

    const FLAG_INSERT = 'insert';

    const FLAG_REPLACE = 'replace';

    const FLAG_VALUES = 'values';

    /**
     * 语句组成部分初始值
     *
     * @access private
     * @var array
     */
    private $_parts = array();

    /**
     * sql关健字
     *
     * @access private
     * @var array
     */
    private $_keys = array(
        self::FLAG_INSERT=> 'INSERT INTO',
        self::FLAG_REPLACE=> 'REPLACE INTO',
        self::FLAG_VALUES=> 'VALUES'
    );

    /**
     * 插入到的表
     *
     * @access private
     * @var string
     */
    private $_table = '';

    /**
     * 插入模式
     *
     * @access private
     * @var string
     */
    private $_flag = 0;

    /**
     * sql语句
     *
     * @access private
     * @var string
     */
    private $_sql = '';

    /**
     * 初始化
     *
     * @access private
     * @return object
     */
    private function _init()
    {
        $this->_parts = array(
            self::FLAG_INSERT=> array(),
            self::FLAG_VALUES=> array()
        );
        return $this;
    }

    /**
     * 构造器
     *
     * @access public
     * @return null
     */
    function __construct()
    {
        $this->_init();
    }

    /**
     * 要插入数据的表
     *
     * @access public
     * @param mixed $table 要插入的表, '字符串或数组'
     * @param array $columns 要插入到哪些字段
     * @return object
     */
    function into($table, $columns)
    {
        $this->_table = is_array($table) ? current($table) : $table;
        $this->_parts[self::FLAG_INSERT] = (array)$columns;
        return $this;
    }

    function flag($flag) {
        if ($flag == self::FLAG_REPLACE) {
            $this->_flag = 1;
        } else {
            $this->_flag = 0;
        }
        return $this;
    }
    /**
     * 要插入的数据
     *
     * @access public
     * @param array $table 要插入的表
     * @return object
     */
    function values($values = array())
    {
        $this->_parts[self::FLAG_VALUES][] = (array)$values;
        return $this;
    }

    /**
     * 拼凑sql语句
     *
     * @access private
     * @return string
     */
    private function _toString()
    {

        $_insert_value = $_insert_fields = array();
        $this->_sql = '';

        if ($this->_parts[self::FLAG_INSERT]) {
             $_insert_fields = implode(', ', $this->_parts[self::FLAG_INSERT]);
        }

        if ($_values = $this->_parts[self::FLAG_VALUES]) {
            foreach ($_values as $key=> $val) {
                $_tmp_values = array_values($val);
                foreach ($_tmp_values as $k=> $v) {
                    $_tmp_values[$k] = $this->quote($v);
                }
                $_insert_value[] = implode(', ', $_tmp_values);
            }
        }

        if ($this->_table && $_insert_fields && $_insert_value) {
            $this->_sql.= ($this->_flag ? $this->_keys[self::FLAG_REPLACE] : $this->_keys[self::FLAG_INSERT]). ' '. $this->_table. '(' . $_insert_fields . ') ' . $this->_keys[self::FLAG_VALUES];
            foreach ($_insert_value as $k=> $v) {
                $_insert_value[$k] = '(' . $v . ')';
            }
            $this->_sql .= implode(', ', $_insert_value);
        }

        return $this->_init();

    }


    /**
     * 字符串输出
     *
     * @access public
     * @return string
     */
    function __toString()
    {
        return $this->_toString()->_sql;
    }

}

?>